package com.javaee.term.service.iml;

import com.javaee.term.Enums.ContactStatusEnum;
import com.javaee.term.Enums.SendProductStatusEnum;
import com.javaee.term.dto.SendProductDetailDto;
import com.javaee.term.model.Contact;
import com.javaee.term.model.Product;
import com.javaee.term.model.SendProductList;
import com.javaee.term.service.SendProductService;
import com.javaee.term.service.base.BaseServiceIml;
import com.javaee.term.utils.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.List;

/**
 * author 陈伯瑞
 * date 2019/12/18 0018 12:49
 * version 1.0
 **/
@Service
public class SendProductServiceImpl extends BaseServiceIml implements SendProductService {
    @Override
    public SendProductList findOne(Object id) {
        return sendProductListMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SendProductList> findAll() {
        return  sendProductListMapper.selectAll();
    }

    @Override
    public void delete(Object id) {
        sendProductListMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SendProductList sendProductList) {
        return sendProductListMapper.insert(sendProductList);
    }

    @Override
    public int update(SendProductList sendProductList) {
        return sendProductListMapper.updateByPrimaryKey(sendProductList);
    }

    @Override
    public List<SendProductList> findByContactIdAndProductId(String contactId, String productId) {
        return sendProductListMapper.selectByContactIdAndProductId(contactId ,productId);
    }



    @Transactional
    @Override
    public void updateQuantityByContactProduct(String contactId, String productId,Integer productQuantity) {
            SendProductList sendProduct = new SendProductList();
            sendProduct.setSendId(KeyUtil.genUniqueKey());
            sendProduct.setProductQuantity(productQuantity);
            sendProduct.setProductId(productId);
            sendProduct.setContactId(contactId);
            sendProductListMapper.insert(sendProduct);
            //插入发货单，合同的状态就变为履行中
        Contact contact=contactMapper.selectByPrimaryKey(contactId);
        contact.setPerformanceStatus(ContactStatusEnum.FULFILLED.getCode());
        contactMapper.updateByPrimaryKey(contact);
    }

    @Override
    public SendProductDetailDto findSendDetail(String sendId) {
        SendProductList sendProductList = sendProductListMapper.selectByPrimaryKey(sendId);
        Product product = productMapper.selectByPrimaryKey(sendProductList.getProductId());
        SendProductDetailDto sendProductDetailDto = new SendProductDetailDto();
        BeanUtils.copyProperties(product,sendProductDetailDto);
        BeanUtils.copyProperties(sendProductList,sendProductDetailDto);
        sendProductDetailDto.setSendProductsMoney(product.getProductPrice().multiply(BigDecimal.valueOf(sendProductList.getProductQuantity())));
        return sendProductDetailDto;
    }

    @Override
    @Transactional
    public void sureSendAndUpdateStock(String sendId) {
        SendProductList one = sendProductListMapper.selectByPrimaryKey(sendId);
        one.setFinshStatus(SendProductStatusEnum.FINISHEDSEND.getCode());
        sendProductListMapper.updateByPrimaryKey(one);
        Product product = productMapper.selectByPrimaryKey(one.getProductId());
        product.setProductStock(product.getProductStock()-one.getProductQuantity());
        productMapper.updateByPrimaryKey(product);
        //确认发货之后，判断合同的所有发货单已发货完成
        List<SendProductList> sendProductLists = sendProductListMapper.selectByContactId(one.getContactId());
        boolean  item=true;//合同履行完
       for( SendProductList each:sendProductLists){
           if(each.getSendProductStatusEnum().equals(SendProductStatusEnum.REQUIRESEND)){
               item=false;
               break;
           }
       }
       if (item){
           //所有发货单已确认跟新合同状态
           Contact contact = contactMapper.selectByPrimaryKey(one.getContactId());
            contact.setPerformanceStatus(ContactStatusEnum.FINISHED.getCode());
            contactMapper.updateByPrimaryKey(contact);
       }
    }

    @Override
    public List<SendProductList> findSendByContactId(String contactId) {
        return sendProductListMapper.selectByContactId(contactId);
    }

}
